package com.servlet.news;

import com.dao.NewsDao;
import com.dao.NewsInfo;
import com.dao.TypeDao;
import com.dao.TypeInfo;
import com.entity.News;
import com.entity.Type;
import com.util.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


/**
 * @author admin
 */
@WebServlet(name = "NewsSelectAllServlet", urlPatterns = "/NewsSelectAllServlet.filter")
public class NewsSelectAllServlet extends HttpServlet {

    private NewsDao newsDao = new NewsInfo();
    private TypeDao typeDao = new TypeInfo();
    private Page page = new Page();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("NewsServlet转发成功");
        String pageNum = request.getParameter("pageNum");
//        String t_name = request.getParameter("t_name");
        try {
            List<News> newsList = newsDao.selectNewsAll(Integer.getInteger(pageNum),page.getPageSize());
            System.out.println(newsList.size());
            System.out.println(newsList.get(0).toString());
            List<Type> typeList = typeDao.typeSelectAll();
            List<List> list = new ArrayList<>();
            list.add(typeList);
            list.add(newsList);
            request.setAttribute("list", list);
            request.getRequestDispatcher("/WEB-INF/page/news.jsp").forward(request, response);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
